package com.openatc.agent.controller.area;

import com.google.gson.JsonObject;
import com.openatc.agent.model.area.Area;
import com.openatc.agent.service.area.AreaService;
import com.openatc.agent.service.impl.WebOperationCollector;
import com.openatc.agent.service.impl.WebOperationRecordExtractor;
import com.openatc.core.model.RESTRetBase;
import com.openatc.core.util.RESTRetUtils;
import com.openatc.extend.common.aop.model.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping(value = "/area")
@RestController
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 新增/更新区域
     * @param areaEntity
     * @return
     */
    @PostMapping
    public RESTRetBase addOrUpdateArea(@RequestBody Area areaEntity) {
        if (areaEntity.getId() == null) {
            return areaService.addArea(areaEntity);
        } else {
            return areaService.renameArea(areaEntity);
        }
    }


    /**
     * 更新区域信息
     * @param areaEntity
     * @return
     */
    @PutMapping
    public RESTRetBase updateAreaInfo(@RequestBody Area areaEntity) {
        return areaService.updateAreaInfo(areaEntity);
    }

    /**
     * 更新协调区域路口信息
     * @param id
     * @param agentids
     * @return
     */
    @PutMapping("/devs/{id}")
    public RESTRetBase updateAreaIsolate(@PathVariable Long id, @RequestBody List<String> agentids) {
        return areaService.updateAreaIsolate(id,agentids);
    }

    /**
     * 更新区域的协调路线信息
     * @param id
     * @param routeIds
     * @return
     */
    @PutMapping("/routes/{id}")
    public RESTRetBase updateAreaRouteIDs(@PathVariable Long id,@RequestBody List<String> routeIds) {
        return areaService.updateAreaRouteIDs(id,routeIds);
    }


    /**
     * 删除区域
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public RESTRetBase deleteArea(@PathVariable Long id) {
        return areaService.deleteArea(id);
    }


    /**
     * 获取所有的区域信息
     * @return
     */
    @GetMapping
    public RESTRetBase getAllAreaInfo() {
        return areaService.getAllArea();
    }

    /**
     * 查询单个的协调区域信息
     * @return
     */
    @GetMapping("{id}")
    public RESTRetBase getAreaInfoById(@PathVariable Long id) {
        return areaService.getAreaInfoById(id);
    }

    /**
     * 获取所有协调区域的简易信息
     * @return
     */
    @GetMapping("simple")
    public RESTRetBase getAreaSimpInfo() {
        return areaService.getAreaSimpleInfo();
    }


    /**
     * 根据区域id获取区域整体方案
     * @param id
     * @return
     */
    @GetMapping("/{feature}/upload/{id}")
    public RESTRetBase getAreaPattern(@PathVariable String feature,@PathVariable Long id) {
        return areaService.getAreaPattern(id,feature);
    }

    /**
     * 整体下载区域方案
     * @param jsonObject
     * @return
     */
    @SysLog(collector = WebOperationCollector.class,extractor = WebOperationRecordExtractor.class)
    @PostMapping("/pattern/download")
    public RESTRetBase saveAreaPattern(@RequestBody JsonObject jsonObject) {
        return areaService.saveAreaPattern(jsonObject);
    }

    /**
     * 子区优化算法
     * @param jsonObject
     * @return
     */
    @PostMapping("/opt/subzone/generate")
    public RESTRetBase optSubZone(@RequestBody JsonObject jsonObject) {
        return RESTRetUtils.successObj("");
    }


    /**
     * 区域优化算法
     * @param jsonObject
     * @return
     */
    @PostMapping("/opt/pattern/generate")
    public RESTRetBase optArea(@RequestBody JsonObject jsonObject) {
        return RESTRetUtils.successObj("");
    }


}
